package com.sky.sky_server.controller.admin;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sky.sky_common.constant.JwtRoleConstant;
import com.sky.sky_common.exception.EmployeeLoginException;
import com.sky.sky_common.result.Result;
import com.sky.sky_common.util.JwtService;
import com.sky.sky_pojo.dto.EmployeeDTO;
import com.sky.sky_pojo.dto.EmployeeLoginVO;
import com.sky.sky_pojo.dto.LoginReq;
import com.sky.sky_pojo.entity.EmployeeQueryPage;
import com.sky.sky_server.service.impl.EmployeeServiceImpl;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Valid
@RestController
@RequestMapping("/admin/employee")
public class EmployeeController {

    @Autowired
    private JwtService jwtService;

    @Autowired
    private EmployeeServiceImpl employeeServiceImpl;

    // todo 课程中的swagger注解，我这里没有，课也没仔细看，ApiOperation注解    
    /**
     * 新增员工信息
     * @param employeeDTO
     * @return
     */
    @PostMapping
    public Result save(@RequestBody EmployeeDTO employeeDTO){
        return employeeServiceImpl.save(employeeDTO) > 0 ? Result.success() : Result.error("新增员工失败");
    }

    // 编辑员工信息
    @PutMapping
    public Result update(@RequestBody EmployeeDTO employeeDTO){
        log.info("编辑员工信息：{}", employeeDTO);
        return employeeServiceImpl.update(employeeDTO) > 0 ? Result.success() : Result.error("编辑员工失败");
    }

    //根据id查询员工
    @GetMapping("/{id}")
    public Result getEmployeeById(@PathVariable Long id){
        return Result.success(employeeServiceImpl.getEmployeeById(id));
    }

    /**
     * 分页查询
     * @param employeeQueryPage
     * @return
     */
    @GetMapping("/page")
    public Result page(@Valid EmployeeQueryPage employeeQueryPage){
        return Result.success(employeeServiceImpl.page(employeeQueryPage));
    }

    /**
     * 修改员工账号状态
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    public Result changeStatus(@PathVariable("status") Integer status, @RequestParam("id") @NotNull Long id){
        log.info("修改员工账号状态{}，{}", status, id);
        employeeServiceImpl.changeStatus(status, id);
        // todo 这里只返回了一个成功
        return Result.success();
    }

    /**
     * 登录
     * @param req
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody @Validated LoginReq req) {
        // TODO 写死登录用户，改为通过查询，校验用户名和密码
        boolean username = !req.getUsername().equals("admin");
        boolean password = !req.getPassword().equals("123456");
        if (username || password) {
            throw new EmployeeLoginException();
        }

        // todo 写死登录用户后面代码，替换为实际的查询结果
        // 自定义载荷
        Map<String, Object> claims = Map.of(
            "username" , req.getUsername(),
            "roles" , new String[]{"admin"},
            "id" , 3L
        );

        String token = jwtService.genJwtToken(req.getUsername(), claims, JwtRoleConstant.ADMIN);
        EmployeeLoginVO requestVO = new EmployeeLoginVO(3, "admin", token, req.getUsername());
        return Result.success(requestVO);
    }

    /**
     * 退出登录
     * @return
     */
    @PostMapping("/logout")
    public Result logout(){
        return Result.success();
    }
}
